package com.dfec.book.controller;


import com.dfec.book.entity.BookCategoryEntity;
import com.dfec.book.service.BookCategoryService;
import com.dfec.book.util.AjaxResult;
import com.dfec.book.util.PageUtils;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 图书类别管理
 * @author tangrg
 * @email 1446232546@qq.com
 * @date 2025-11-19 11:21:46
 */
@RestController
@RequestMapping("/bookCategory")
@Api(tags = "图书类别管理")
@ApiSort(1)
public class BookCategoryController {

    @Resource
    private BookCategoryService bookCategoryService;

    /**
     * 分页查询图书类别
     */
    @PostMapping("/page")
    @ApiOperation("分页查询图书类别")
    public AjaxResult<PageUtils> getBookCategoryPage(
            @ApiParam(value = "图书类别查询参数", required = true)
            @RequestBody BookCategoryEntity bookCategoryEntity) {

        PageUtils page = bookCategoryService.getBookCategoryPage(bookCategoryEntity);
        return AjaxResult.ok(page);
    }

    /**
     * 获取所有图书类别
     */
    @GetMapping("/all")
    @ApiOperation("获取所有图书类别")
    public AjaxResult<List<BookCategoryEntity>> getAllCategories() {
        List<BookCategoryEntity> categories = bookCategoryService.getAllCategories();
        return AjaxResult.ok(categories);
    }

    /**
     * 添加新类别
     */
    @PostMapping("/add")
    @ApiOperation("添加新类别")
    public AjaxResult<Boolean> addCategory(
            @ApiParam(value = "图书类别实体", required = true)
            @RequestBody BookCategoryEntity category) {

        boolean result = bookCategoryService.addCategory(category);
        return result ? AjaxResult.ok() : AjaxResult.error("添加失败");
    }

    /**
     * 根据ID删除类别
     */
    @PostMapping("/delete")
    @ApiOperation("根据ID删除类别")
    public AjaxResult<Boolean> deleteCategory(
            @ApiParam(value = "类别ID", required = true)
            @RequestBody List<Integer> categoryIds) {

        try {
            boolean result = bookCategoryService.deleteCategory(categoryIds);
            return result ? AjaxResult.ok() : AjaxResult.error("删除失败");
        } catch (RuntimeException e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 根据名称查找类别
     */
    @GetMapping("/findByName")
    @ApiOperation("根据名称查找类别")
    public AjaxResult<BookCategoryEntity> findCategoryByName(
            @ApiParam(value = "类别名称", required = true)
            @RequestParam String categoryName) {

        BookCategoryEntity category = bookCategoryService.findCategoryByName(categoryName);
        return AjaxResult.ok(category);
    }

    /**
     * 更新类别信息
     */
    @PostMapping("/update")
    @ApiOperation("更新类别信息")
    public AjaxResult<Boolean> updateCategory(
            @ApiParam(value = "图书类别实体", required = true)
            @RequestBody BookCategoryEntity category) {

        try {
            boolean result = bookCategoryService.updateCategory(category);
            return result ? AjaxResult.ok() : AjaxResult.error("更新失败");
        } catch (RuntimeException e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 检查类别是否存在
     */
    @GetMapping("/exists")
    @ApiOperation("检查类别是否存在")
    public AjaxResult<Boolean> existsById(
            @ApiParam(value = "类别ID", required = true)
            @RequestParam Integer categoryId) {
        boolean exists = bookCategoryService.existsById(categoryId);
        return AjaxResult.ok(exists);
    }
}